#!/usr/bin/python
# vi: et sw=2
#
# Control class

import pygtk
import gtk
import gobject

from AppletGConfig import *

from AppletView import *
from PulseGlib  import *

# Controller
class PulseApplet(gtk.Image):
  def __init__(self, applet, iid):
    gtk.Image.__init__(self)
    self.set_from_stock(gtk.STOCK_DISCONNECT, gtk.ICON_SIZE_MENU)

    self.applet   = applet
    self.iid      = iid

    self.settings = AppletGConfig()
    self.settings.connect(
        ["layout", "theme", "use_xml_icons"],
        self.update_view_settings
    )   
    
    self.model    = AppletModel()
    self.view     = AppletView(self.settings.get_view_settings(), self.model)

    self.pulse   = PulseGlib()
    self.pulse.connect("context-ready", self.on_pulse_context_ready)
    self.pulse.connect("context-failed", self.on_pulse_context_failed)
    self.pulse.connect("context-terminated", self.on_pulse_context_terminated)

    self.applet.add(self)

    self.applet.connect("button-press-event", self.button_press_event)

    self.pulse.pulseConnect()

    self.applet.show_all()
    self.show()

    print "PulseApplet is now loaded with", iid
    return

    #
    # __init__ end
    #

  #
  # PulseAudio events

  def on_pulse_context_ready(self, pulse):
    self.set_from_stock(gtk.STOCK_CONNECT, gtk.ICON_SIZE_MENU)
    self.model.set_pulse(pulse)
    pass
 
    # 
    # on_pulse_context_ready end
    #

  def on_pulse_context_failed(self, pulse):
    self.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_MENU)
    pass

    #
    # on_pulse_context_failed end
    #

  def on_pulse_context_terminated(self, pulse):
    self.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_MENU)
    pass
 
    # 
    # on_pulse_context_terminated end
    #

  #
  # Applet events

  def button_press_event(self, widget, event):
    (appletX, appletY) = self.applet.window.get_origin()
    (appletW, appletH) = self.applet.window.get_size()

    self.view.move(appletX, appletY+appletH)

    if self.view.get_property("visible") == True:
      self.view.hide()
    else:
      self.view.show_all()
    pass

    #
    # button_press_event end
    #

  #
  # Settings events

  def update_view_settings(self, client, *args, **kwags):
    self.view.update_settings(self.settings.get_view_settings())

    #
    # update_view_settings end
    #

  pass

  #
  # class PulseApplet end
  #
